package com.qf.test;

import com.qf.util.ElasticsearchUtil;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Demo1 {

    /**
     * REST API 和 Elasticsearch服务交互
     */
    @Test
    public void index() throws IOException {
        //1. 创建指定的Request对象
        IndexRequest request = new IndexRequest();

        //2. 封装数据到Request , 具体索引,文档id等参数
        request.index("book");
        request.id("3");
        Map source = new HashMap();
        source.put("name","深入了解JVM虚拟机");
        source.put("author","周阿巴");
        source.put("count",500000);
        source.put("publish_date",new Date());
        source.put("country","牙买加");
        source.put("money",66);
        request.source(source);

        //3. 通过RestHighLevelClient将Request对象发送到ES服务,并接收ES服务的Response
        IndexResponse resp = ElasticsearchUtil.getClient().index(request, RequestOptions.DEFAULT);

        //4. 输出结果
        System.out.println(resp.getResult().toString());
    }


    @Test
    public void update() throws IOException {
        //1. 创建指定的Request对象
        UpdateRequest request = new UpdateRequest();

        //2. 封装数据到Request , 具体索引,文档id等参数
        request.index("book");
        request.id("3");
        Map<String, Object> source = new HashMap<>();
        source.put("name","狂神");
        request.doc(source);

        //3. 通过RestHighLevelClient将Request对象发送到ES服务,并接收ES服务的Response
        UpdateResponse resp = ElasticsearchUtil.getClient().update(request, RequestOptions.DEFAULT);

        //4. 输出结果
        System.out.println(resp.getResult().toString());
    }


    @Test
    public void bulk() throws IOException {
        //1. 创建指定的Request对象
        BulkRequest request = new BulkRequest();

        //2. 封装数据到Request , 具体索引,文档id等参数
        //创建指定的Request对象
        IndexRequest indexRequest1 = new IndexRequest();
        //2封装数据到Request , 具体索引,文档id等参数
        indexRequest1.index("book");
        indexRequest1.id("4");
        Map source1 = new HashMap();
        source1.put("name","深入");
        source1.put("author","周阿巴");
        source1.put("count",50000);
        source1.put("publish_date",new Date());
        source1.put("country","葡萄牙");
        source1.put("money",77);
        indexRequest1.source(source1);

        //创建指定的Request对象
        IndexRequest indexRequest2 = new IndexRequest();
        //2封装数据到Request , 具体索引,文档id等参数
        indexRequest2.index("book");
        indexRequest2.id("5");
        Map source2 = new HashMap();
        source2.put("name","深了解");
        source2.put("author","阿巴周");
        source2.put("count",5000);
        source2.put("publish_date",new Date());
        source2.put("country","利比亚");
        source2.put("money",78);
        indexRequest2.source(source2);

        request.add(indexRequest1);
        request.add(indexRequest2);

        //3. 通过RestHighLevelClient将Request对象发送到ES服务,并接收ES服务的Response
        BulkResponse resp = ElasticsearchUtil.getClient().bulk(request, RequestOptions.DEFAULT);

        //4. 输出结果
        for (BulkItemResponse item : resp.getItems()) {
            System.out.println(item.getResponse().getResult().toString());
        }
    }


}
